export default{
    install(Vue,options)
    {

        Vue.prototype.Network = function (url,data,callback) {
            this.$http.jsonp(url,{jsonp:'callback',params:data}).then(function(res){
                callback(res);
            })
        };
        Vue.prototype.Posthttp = function (url,data,callback, errCallback) {
            // 2019.6.18 登录接口不允许使用member_id
            if (url.indexOf('/?method=flower.login.index') === -1) {
              // 2019.6.18 通过和后端商量所有请求都加上member_id
              data.member_id = this.getMemberId()
            }
            this.$http.post('http://hyxy.api.huawa.com'+url,data,{emulateJSON:true}).then(function (res){
                if(!res.bodyText)return
                //登陆失效统一提示跳转
                if(JSON.parse(res.bodyText).is_expire === 1){
                    // 2019.6.28 由于不明原因会清掉app的用户信息，所以决定不再调用app的清除方法
                    // if(this.deviceType() === 1){//安卓
                    //     window.hy.exitApp()
                    // }else if( this.deviceType() === 2 ){//IOS
                    //     window.webkit.messageHandlers.exitApp.postMessage(null) 
                    // }

                    if(this.$route.path === '/bag'){
                        this.$router.push('/bagLogout')
                    }else{
                        this.$router.push('/login')  
                    }

                    this.$message.warning('账号已过期或在其他地方登录，请重新登录~~')
                    window.localStorage.removeItem('userInfo')

                    return

                }else if(JSON.parse(res.bodyText).is_expire === 10000){//未登录提示 不跳转

                    window.localStorage.removeItem('userInfo')
                    this.$router.push('/login')
                    return;
                }
                callback(res);
            }).catch(err => {
                errCallback(err)
            })
        };
        //获取用户member_id
        Vue.prototype.getMemberId = function(){
            let userInfo = JSON.parse(window.localStorage.getItem('userInfo')),
                member_id = userInfo?userInfo.token:0;
            return member_id;
        }

        //判断是否是微信浏览器
        Vue.prototype.isWeiXin = function(){
            let ua = window.navigator.userAgent.toLowerCase();
            if(ua.match(/MicroMessenger/i) == 'micromessenger'){
                return true;
            }else{
                return false;
            }
        }

        /*
-----------app相关方法 start ---------
*/

        //app端设备类型
        Vue.prototype.deviceType = function(){
            let data = JSON.parse(window.localStorage.getItem('userApp')) || {};
            return parseInt(data.deviceType);
        };
        //app返回事件
        Vue.prototype.appBack = function(){
            parseInt(this.deviceType()) === 1? window.hy.exit():window.webkit.messageHandlers.hyExit.postMessage(null);
        };

        /**
         * 设备类型
         * @param 1.android App
         * @param 2.ios App
         * @param 3.wechat
         * @param 4.wap
         */
        Vue.prototype.deviceType = function () {

            let ua = window.navigator.userAgent.toLowerCase()

            if ( ua.match(/iOS_APP/i) == 'ios_app' ) {
                return 2
            } else if ( ua.match(/ANDROID_APP/i) == 'android_app' ) {
                return 1
            } else if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                return 3
            } else {
                return 4
            }

        }
        /**
         * 是否在app中
         * @param value
         */
        Vue.prototype.inApp = function () {
            let ua = window.navigator.userAgent.toLowerCase()
            return ua.match(/_APP/i) == '_app'?true:false
        }
        /**
         * 关闭花材商城回到花娃
         * @param window.hcsc.hcscExit() - android
         * @param window.webkit.messageHandlers.hcscExit.postMessage(null) - ios
         */
        Vue.prototype.closeStation = function () {

            let type = this.deviceType()
            switch(type){
                case 1:
                    window.webkit.messageHandlers.hcscExit.postMessage(null)
                    break;
                case 2:
                    window.hcsc.hcscExit()
                    break;
                default:
                    break;
            }

        }

        /*
------------ app相关方法 end  --------
*/

        //动画延迟
        Vue.prototype.delay = function (index,noPages) {
            if(noPages){//没有分页功能时，全部延迟动画
                return (index+1)/10+'s';
                return;
            }
            if(index>=6){//超出分页数外的数据不要延迟
                return '0s';
            }else{//分页数内的数据延迟动画
                return (index+1)/10+'s';
            }
        };
        //-本地储存-用户未登录的分享次数
        Vue.prototype.setOffline = function (item,key,value) {
            let data ={}
            if(window.localStorage.getItem(item)){
                data = JSON.parse(window.localStorage.getItem(item)) 
            }
            data[key] = value
            window.localStorage.setItem(item,JSON.stringify(data))
        };
        //返回上个页面
        Vue.prototype.goback = function (index) {
            if(!index)index=-1;
            this.$router.go(index);
        };
        //课程列表路由 query 处理
        Vue.prototype.routeData = function () {
            //路由跳转时没有传值的 统一赋值为0
            this.$route.query.cat_id = this.$route.query.cat_id?this.$route.query.cat_id:0;
            this.$route.query.keyword = this.$route.query.keyword?this.$route.query.keyword:'';
            this.$route.query.is_recommend = this.$route.query.is_recommend?this.$route.query.is_recommend:0;
            this.$route.query.is_free = this.$route.query.is_free?this.$route.query.is_free:0;
            this.$route.query.order = this.$route.query.order?this.$route.query.order:0;
            this.$route.query.lession_type = this.$route.query.lession_type?this.$route.query.lession_type:0;
            this.$route.query.teacher_type = this.$route.query.teacher_type?this.$route.query.teacher_type:0;
            this.$route.query.page = this.$route.query.page?this.$route.query.page:1;
            this.$route.query.teacher_id = this.$route.query.teacher_id?this.$route.query.teacher_id:0;
            //传给后台的值
            return  {
                cat_id:this.$route.query.cat_id,
                keyword:this.$route.query.keyword,
                is_recommend:this.$route.query.is_recommend,
                is_free:this.$route.query.is_free,
                order:this.$route.query.order,
                lession_type:this.$route.query.lession_type,
                teacher_type:this.$route.query.teacher_type,
                page:this.$route.query.page,
                teacher_id:this.$route.query.teacher_id,
            }
        };
        //课程详情页路由 query 处理
        Vue.prototype.detailRoute = function () {
            //路由跳转时没有传值的 统一赋值为0
            this.$route.query.lession_id = this.$route.query.lession_id?this.$route.query.lession_id:0;
            this.$route.query.video_id = this.$route.query.video_id?this.$route.query.video_id:0;
            //传给后台的值
            return  {
                lession_id:this.$route.query.lession_id,
                member_id:this.getMemberId(),
                video_id:this.$route.query.video_id,
            }
        }
        /**
         * 获取隐藏元素属性
         * @param
         * @return null
         */
        Vue.prototype.getDefaultStyle = function (obj, attribute) { // 返回最终样式函数，兼容IE和DOM，设置参数：元素对象、样式特性
            if (!obj) return;
            return obj.currentStyle ? obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj, false)[attribute]
        }

      /**
       * 判断是否登录
       * @param
       */
      Vue.prototype.loginState = function(){

        let userInfo = JSON.parse(window.localStorage.getItem('userInfo')),
          member_id = userInfo?userInfo.token:0

        return member_id === 0 ? false : true
      }
      /**
       * ios内购判断
       * @param
       */
      Vue.prototype.iosStoreBuy = function(){

        this.$router.push('/bag/deposit')
        return
        /*IOS内购判断*/
        if(this.appType()===2){
          let data = this.getMemberId()
          window.webkit.messageHandlers.hyBuyFlower.postMessage(`${data}`)
          this.$router.go(-1)
        }else{
          this.$router.push('/bag/deposit')
        }
      }
    }
}
